package com.jobsleaf.selection.provider;

import java.util.HashMap;
import java.util.List;

import org.eclipse.core.runtime.ListenerList;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.part.ViewPart;

public class PersonView extends ViewPart implements ISelectionProvider
{
	private TableViewer mViewer;
	ListenerList listeners = new ListenerList();
	HashMap<String, String> address;

	private void createTableViewer(Composite parent)
	{
		mViewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
		mViewer.setLabelProvider(new TableLabelProvider());
		mViewer.setContentProvider(new MyContentProvider());
		String[] columnHeading = new String[] { "Name", "Date Of Birth", "Father Name", "Email id" };
		Table table = mViewer.getTable();
		for (int i = 0; i < columnHeading.length; i++)
		{
			TableColumn tab = new TableColumn(table, SWT.BOLD);
			tab.setText(columnHeading[i]);
			tab.setWidth(100);
		}
		getSite().setSelectionProvider(this);
		mViewer.addSelectionChangedListener(new ISelectionChangedListener()
		{
			@Override
			public void selectionChanged(SelectionChangedEvent event)
			{
				Person person = (Person) ((StructuredSelection) event.getSelection()).getFirstElement();
				setSelection(new StructuredSelection(person.getAdress()));
			}
		});

	}

	public void refresh(List<Person> list)
	{
		mViewer.getTable().clearAll();
		mViewer.setInput(list.toArray());
	}

	public void setFocus()
	{

	}

	public void addSelectionChangedListener(ISelectionChangedListener listener)
	{
		listeners.add(listener);
	}

	public ISelection getSelection()
	{
		return new StructuredSelection("");
	}

	public void removeSelectionChangedListener(ISelectionChangedListener listener)
	{
		listeners.remove(listener);
	}

	public void setSelection(ISelection select)
	{

		Object[] list = listeners.getListeners();
		for (int i = 0; i < list.length; i++)
		{
			((ISelectionChangedListener) list[i]).selectionChanged(new SelectionChangedEvent(this, select));
		}
	}

	@Override
	public void createPartControl(Composite parent)
	{
		createTableViewer(parent);
		List<Person> list = TableUtility.getAuditList();
		mViewer.setInput(list.toArray());
		mViewer.getTable().setHeaderVisible(true);
		mViewer.getTable().setLinesVisible(true);

		final MenuManager mm = new MenuManager("view.popupmenu");
		final Menu menu = mm.createContextMenu(mViewer.getTable());
		mViewer.getTable().setMenu(menu);
		getSite().registerContextMenu(mm, mViewer);
	}
}
